//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
/******************************************************************************
**
** INTEL CONFIDENTIAL
** Copyright 2003-2005 Intel Corporation All Rights Reserved.
**
** The source code contained or described herein and all documents
** related to the source code (Material) are owned by Intel Corporation
** or its suppliers or licensors.  Title to the Material remains with
** Intel Corporation or its suppliers and licensors. The Material contains
** trade secrets and proprietary and confidential information of Intel
** or its suppliers and licensors. The Material is protected by worldwide
** copyright and trade secret laws and treaty provisions. No part of the
** Material may be used, copied, reproduced, modified, published, uploaded,
** posted, transmitted, distributed, or disclosed in any way without Intel's
** prior express written permission.
**
** No license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise. Any license under such intellectual property rights
** must be express and approved by Intel in writing.
**
**
******************************************************************************/

/***********************************************************
*    SDMMC.h
*       This is the header file for the SDMMC driver that 
*       is used with eboot to load images off of SD/MMC 
*       cards.
*
*    Revision History:
*       Jason White - 1/4/06 - initial draft
************************************************************/

#include "xllp_mmc.h"
#include "xllp_clkmgr.h"
#include "monahans_base_regs.h"
#include <oal_memory.h>
#include <monahans.h>

#define TRUE                  1
#define FALSE                 0

#define OSCR_OFFSET           0x10

#define BOOL                  int

#define RCA                   0x0005ffff
#define OCR                   0x80FF8000

#define SD_OCR                0x00FF8000

#define READ_FIFO_MASK        0x000000ff

#define RESPONSE_LENGTH       8
#define FIFO_LENGTH           32

#define OCR_RESERVED          0xff
#define UPPER_BITS            0xff00

#define MMC_BUSY_BIT          0x3F80
#define SD_NO_CMD1            0x300

#define LOW_ARG               0x0000ffff
#define NO_ARGUMENT           0xffffffff
#define NO_FLAGS              0
#define BLK_CNT               1
#define BLK_LEN               512

typedef	enum
{
    XLLP_MMC_CMD0                = 0x0,	// go idle state
    XLLP_MMC_CMD1                = 0x1,	// send op command
    XLLP_MMC_CMD2                = 0x2,	// all send cid
    XLLP_MMC_CMD3                = 0x3,	// set relative addr
    XLLP_MMC_CMD4                = 0x4,	// set dsr
    XLLP_MMC_CMD7                = 0x7,	 // select/deselect card
    XLLP_MMC_SPICMD9             = 0x9,	// send csd
    XLLP_MMC_SPICMD10            = 0xa,	// send cid
    XLLP_MMC_CMD11               = 0xb,	//  read data until stop
    XLLP_MMC_CMD12               = 0xc,	 // stop transmission
    XLLP_MMC_CMD13               = 0xd,	 // send status
    XLLP_MMC_CMD15               = 0xf,	// go inactive state
    XLLP_MMC_CMD16               = 0x10,	//  set block length
    XLLP_MMC_CMD17               = 0x11,	// read single block
    XLLP_MMC_CMD18               = 0x12,	//  read multiple block
    XLLP_MMC_CMD20               = 0x14,	 // write data until stop
    XLLP_MMC_SPICMD24            = 0x18,	 // write block
    XLLP_MMC_CMD25               = 0x19,	 // write multiple block
    XLLP_MMC_CMD26               = 0x1a,	// program CID
    XLLP_MMC_SPICMD27            = 0x1b,	// program CSD
    XLLP_MMC_SPICMD28            = 0x1c,	// set write prot
    XLLP_MMC_SPICMD29            = 0x1d,  // clr write prot
    XLLP_MMC_SPICMD30            = 0x1e,	// send write prot
    XLLP_MMC_SPICMD32            = 0x20,	 // tag sector start
    XLLP_MMC_SPICMD33            = 0x21,	// tag sector end
    XLLP_MMC_SPICMD34            = 0x22,	// untag sector
    XLLP_MMC_SPICMD35            = 0x23,	// tag erase group start
    XLLP_MMC_SPICMD36            = 0x24,	//  tag erase group end
    XLLP_MMC_SPICMD37            = 0x25,	 // untag erase group
    XLLP_MMC_SPICMD38            = 0x26,	 // erase
    XLLP_MMC_CMD39               = 0x27,	//fast IO
    XLLP_MMC_CMD40               = 0x28,	// go irq state
    XLLP_MMC_SPICMD42            = 0x2a,	// lock-unlock
    XLLP_MMC_CMD55               = 0x37,	// app cmd
    XLLP_MMC_SPICMD56            = 0x38,	//  gen cmd
    XLLP_SPI_CMD58               = 0x3a,	 // read ocr
    XLLP_SPI_CMD59               = 0x3b,	 // crc on-off
    XLLP_MMC_CMD60               = 0x3c,	// reserved for manufacturer
    XLLP_MMC_CMD61               = 0x3d,	 // reserved for manufacturer
    XLLP_MMC_CMD62               = 0x3e,	// reserved for manufacturer
    XLLP_MMC_CMD63               = 0x3f,	 // reserved for manufacturer
    XLLP_SD_ACMD6                = 0x146,
    XLLP_SD_ACMD13               = 0x14D,
    XLLP_SD_ACMD22               = 0x156,
    XLLP_SD_ACMD23               = 0x157,
    XLLP_SD_ACMD41	             = 0x169,
    XLLP_SD_ACMD42	             = 0x16a,
    XLLP_SD_ACMD51      	     = 0x173
} XLLP_MMC_CMD;

//function definitions
XLLP_UINT32_T SDMMCInit(P_XLLP_MMC_CONTEXT_T pContext, P_XLLP_CLKMGR_T clkmgr);
BOOL SDMMCIsCardInserted(P_XLLP_MMC_CONTEXT_T pContext);
void SDMMCReadBlock(P_XLLP_MMC_CONTEXT_T pContext, 
                    XLLP_UINT32_T block, BYTE *buffer);
XLLP_UINT32_T SDMMCCardInit(P_XLLP_MMC_CONTEXT_T pContext);
void SDMMCMaskInterrupts(P_XLLP_MMC_CONTEXT_T pContext);
void SDMMCSendCommand(P_XLLP_MMC_CONTEXT_T pContext, XLLP_UINT32_T cmd, 
                      XLLP_UINT32_T argument, XLLP_UINT32_T flags);
XLLP_UINT32_T SDMMCGetResponse(P_XLLP_MMC_CONTEXT_T pContext, 
                               XLLP_UINT32_T RespType, BOOL in_while);
void SDMMCReadFifo(P_XLLP_MMC_CONTEXT_T pContext, BYTE *buffer);
void SDMMCSetBlockLength(P_XLLP_MMC_CONTEXT_T pContext, XLLP_UINT32_T length);
void SDMMCGetCardStatus(P_XLLP_MMC_CONTEXT_T pContext, XLLP_UINT32_T rca);
void SDMMCPrintFifo(BYTE *buffer);

static void Wait(UINT32 microSeconds);
static void msWait(UINT32 msVal);
